package org.dromara.system.dubbo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.system.api.RemoteStoreService;
import org.dromara.system.api.domain.vo.RemoteStoreVo;
import org.dromara.system.service.ISysStoreService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 抽帧服务
 *
 * @author samuel
 * @date 2024-03-27 20:36:33
 */

@Service
@DubboService
@RequiredArgsConstructor
public class RemoteStoreServiceImpl implements RemoteStoreService {

    private final ISysStoreService sysStoreService;

    @Override
    public String selectNameByStoreId(Long storeId) {
        List<RemoteStoreVo> remoteStoreVos = sysStoreService.selectNamesByStoreList(CollUtil.newArrayList(storeId));
        return CollUtil.isEmpty(remoteStoreVos) ? null : remoteStoreVos.get(0).getStoreName();
    }

    @Override
    public  Map<Long, String> selectNamesByStoreList(List<Long> storeIds) {
        if (CollUtil.isEmpty(storeIds)) {
            return MapUtil.newHashMap();
        }
        List<RemoteStoreVo> remoteStoreVos = sysStoreService.selectNamesByStoreList(storeIds);
        return remoteStoreVos.stream()
                .collect(Collectors.toMap(
                        RemoteStoreVo::getStoreId,
                        store -> Optional.ofNullable(store.getStoreName()).orElse(""),
                        (existing, replacement) -> existing // 或其他合并策略
                ));
    }

    /**
     * 根据给定的店铺ID列表，查询对应的上级区域列表
     *
     * @param storeIds 店铺ID列表
     * @return 包含店铺名称的RemoteStoreVo对象列表
     */
    @Override
    public Map<Long, String> selectGroupNamesByStoreList(List<Long> storeIds) {
        if (CollUtil.isEmpty(storeIds)) {
            return MapUtil.newHashMap();
        }
        List<RemoteStoreVo> remoteStoreVos = sysStoreService.selectGroupNamesByStoreList(storeIds);
        return remoteStoreVos.stream()
                .collect(Collectors.toMap(
                        RemoteStoreVo::getStoreId,
                        store -> Optional.ofNullable(store.getGroupName()).orElse(""),
                        (existing, replacement) -> existing // 或其他合并策略
                ));
    }

    /**
     * 根据门店Id查询上级区域id
     *
     * @param storeId
     * @return
     */
    @Override
    public Long selectGroupIdByStoreId(Long storeId) {
        if(ObjectUtil.isNull(storeId)){
            throw new ServiceException("门店id不能为空");
        }
       return sysStoreService.selectGroupIdByStoreId(storeId);
    }

    /**
     * 根据区域id获取门店列表
     *
     * @param groupIds
     * @return
     */
    @Override
    public List<RemoteStoreVo> queryStoreByGroupIds(List<Long> groupIds) {
        return sysStoreService.queryStoreByGroupIds(groupIds);
    }

    /**
     * 根据门店ids查询门店是否存在
     *
     * @param storeIds
     * @return
     */
    @Override
    public Map<Long, Boolean> queryIsStore(List<Long> storeIds) {
        return sysStoreService.queryIsStore(storeIds);
    }

    /**
     * 获取全部的门店
     *
     * @return
     */
    @Override
    public List<RemoteStoreVo> queryStore() {
        return sysStoreService.queryStore();
    }
}
